package com.dule.Medic.pages;

import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.dule.Medic.beans.PrescriptionBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Patient;
import com.dule.Medic.model.Prescription;
import com.dule.Medic.service.MedEntryService;
import com.dule.Medic.service.PrescriptionService;

public class ChangeMedEntryData
{
	@SessionState      
	@Property    
	private Doctor doctor;     
	private boolean doctorExists;    
	
	@SessionState 
	@Property
	private Patient patient;
	private boolean patientExists;
	
	@SessionState
	@Property
	private MedEntry medentry;
	private boolean medentryExists;
	
	@SuppressWarnings("unused")
	@SessionState
	@Property
	private Prescription prescription;
	
	@InjectComponent
	@Property
	private Zone PrescriptionsTable;
	
	@Inject
	private PrescriptionService prescriptionService;
	@Inject       
	private MedEntryService medentryservice; 
	
	public Object onActivate()
	{   
		if(doctorExists)
		{
			if(patientExists && medentryExists)
			{
				prescription=new PrescriptionBean();
				return null;
			}
			else
			{
				return Index.class;
			}
		}        
		return FirstPage.class;   
	}
	
	@OnEvent(component="changeprescription")
    public Object Onchangeprescription(Long Id)
    {
		prescription= medentry.getPrescriptionById(Id);
		return ChangePrescriptionData.class;
    } 
	
	@OnEvent(component="medentryform")  
	public Object OnChangeMedEntryForm()
	{   
		
		doctor.UpdateMedEntry(medentry);
		patient.UpdateMedEntry(medentry);
		medentryservice.update(medentry);
		return ExaminePatient.class;  
	} 
	
	@OnEvent(component="deleteprescription")
    public Object OnDeletePrescription(Long Id)
    {
		
		medentry.getPrescriptions().remove(medentry.getPrescriptionById(Id));
		medentryservice.update(medentry);
		prescriptionService.delete(prescriptionService.retrieveOne(Id));
		return PrescriptionsTable.getBody();
    } 
	
	
	
}
